package com.zzsy.shoppingpro.user.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.base.BaseActivity;
import com.zzsy.shoppingpro.basebean.BaseBean;
import com.zzsy.shoppingpro.custom.CircleImageView;
import com.zzsy.shoppingpro.http.BaseCallback;
import com.zzsy.shoppingpro.http.RetrofitHelper;
import com.zzsy.shoppingpro.server.MainService;
import com.zzsy.shoppingpro.user.bean.UserInfoBean;
import com.zzsy.shoppingpro.utils.SPHelper;
import com.zzsy.shoppingpro.utils.StringUtils;
import com.zzsy.shoppingpro.utils.Toast;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyInfoActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvTitle;
    private LinearLayout llSetHeader;
    private CircleImageView cirImg;
    private EditText etUserName;
    private TextView tvPhone;
    private TextView phone;
    private RelativeLayout rlBirthday;
    private EditText etCardNum;
    private TextView tvSaveInfo;
    private TextView tvShowbirday;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        initUI();
    }

    /**
     * 初始化数据
     */
    private void initUI() {
        tvTitle = findViewById(R.id.toolbar_title);
        tvTitle.setText("个人资料");
        //设置用户头像
        llSetHeader = findViewById(R.id.ll_set_user_header);
        llSetHeader.setOnClickListener(this);
        //头像
        cirImg = findViewById(R.id.img_header);
        //设置用户名
        etUserName = findViewById(R.id.et_user_name);
        //修改手机号按钮
        tvPhone = findViewById(R.id.tv_update_phone);
        //手机号显示控件
        phone = findViewById(R.id.tv_phone);
        //设置生日
        rlBirthday = findViewById(R.id.rl_set_birthday);
        rlBirthday.setOnClickListener(this);
        //车牌号
        etCardNum = findViewById(R.id.et_card_num);
        //保存
        tvSaveInfo = findViewById(R.id.tv_save_info);
        //生日
        tvShowbirday = findViewById(R.id.tv_show_birthday);

    }

    @Override
    protected void onResume() {
        super.onResume();
            getUserInfoData();
    }

    /**
     * 获取用户数据
     */
    private void getUserInfoData() {

        RetrofitHelper
                .create(MainService.class)
                .getUserInfoData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseCallback<BaseBean<UserInfoBean>>() {
                    @Override
                    public void success(BaseBean<UserInfoBean> userInfoBeanBaseBean) {
                        if (userInfoBeanBaseBean.getCode() != 200) {
                            Toast.show(MyInfoActivity.this, userInfoBeanBaseBean.getMsg());
                            return;
                        }

                        String name = userInfoBeanBaseBean.getData().getName();
                        // 用户头像
                        String headImgUrl = userInfoBeanBaseBean.getData().getHeadImgUrl();
                        // 电话号
                        String phoneStr = userInfoBeanBaseBean.getData().getPhone();
                        // 车牌号
                        String plateNumber = userInfoBeanBaseBean.getData().getPlateNumber();
                        //会员生日
                        String birthday = userInfoBeanBaseBean.getData().getBirthday();

                        setUserUI(name, headImgUrl, phoneStr, plateNumber, birthday);


                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    /**
     * 设置ui数据
     */
    private void setUserUI(String name, String headImgUrl, String phoneStr, String plateNumber, String birthday) {

        if (StringUtils.isEmpty(headImgUrl)) {
            cirImg.setImageResource(R.drawable.head_normal);
        } else {
            RequestOptions myOptions = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.head_normal);
            Glide.with(MyInfoActivity.this)
                    .load(headImgUrl)
                    .apply(myOptions)
                    .into(cirImg);
        }

        if (StringUtils.isEmpty(birthday)) {
            tvShowbirday.setText("");
        } else {
            tvShowbirday.setText(birthday);
        }

        if(StringUtils.isEmpty(name)){
            etUserName.setText("");
        }else{
            etUserName.setText(name);
        }

        if(StringUtils.isEmpty(phoneStr)){
            phone.setText("请输入电话号");
        }else{
            phone.setText(phoneStr);
        }

        if(StringUtils.isEmpty(plateNumber)){
            etCardNum.setText("");
        }else{
            etCardNum.setText(plateNumber);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            //设置头像
            case R.id.ll_set_user_header:
                break;
            //设置生日
            case R.id.rl_set_birthday:
                break;
        }

    }
}
